package net.posick.ws.xml;

import java.io.Serializable;

//import net.posick.ws.Constants;

public class Name implements Serializable
{
    private static final long serialVersionUID = 201403191301L;
    
//    protected static final String LOG_TAG = Constants.LOG_TAG + "." + Name.class.getSimpleName();
    
    protected String name;
    
    protected String string = null;
    
    protected Integer hashCode = null;

    private Namespace namespace = null;
    
    
    public Name(Namespace namespace, String name)
    {
        super();
        this.namespace = namespace;
        this.name = name;
    }
    
    
    public Name(String prefix, String name)
    {
        super();
        this.namespace = new Namespace(prefix, null);
        this.name = name;
    }
    
    
    public Name(String fqn)
    {
        super();
        String prefix = null;
        String name = null;
        int pos = -1;
        if ((pos = fqn.indexOf(':')) >= 0)
        {
            // Fully Qualified Name contains a Namespace specification
            prefix = fqn.substring(0, pos);
            if (fqn.length() >= pos + 1)
            {
                name = fqn.substring(pos + 1);
            }
        } else
        {
            // Fully Qualified Name does not contain a Namespace specification
            name = fqn;
        }
        
        this.namespace = new Namespace(prefix, null);
        this.name = name;
    }


    public Namespace getNamespace()
    {
        return namespace;
    }


    public void setNamespace(Namespace namespace)
    {
        this.namespace = namespace;
    }


    public String getPrefix()
    {
        return namespace.getPrefix();
    }


    public void setPrefix(String prefix)
    {
        if (namespace == null)
        {
            namespace = new Namespace(prefix, null);
        } else
        {
            namespace.setPrefix(prefix);
        }
    }


    public String getName()
    {
        return name;
    }


    public void setName(String name)
    {
        this.name = name;
    }
    
    
    @Override
    public int hashCode()
    {
        if (hashCode == null)
        {
            hashCode = Integer.valueOf(toString().hashCode());
        }
        
        return hashCode.intValue();
    }
    
    
    @Override
    public String toString()
    {
        if (string == null)
        {
            String prefix = namespace.getPrefix();
            string = (prefix != null ? prefix + ":" : "") + (name != null ? name : "");
        }
        
        return string;
    }
    
    
    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        } else if (o instanceof Name)
        {
            Name that = (Name) o;
            
            String thisName = this.name;
            String thatName = that.name;
            String thisPrefix = this.namespace.getPrefix();
            String thisNamespaceUri = this.namespace.getUri();
            String thatPrefix = that.namespace.getPrefix();
            String thatNamespaceUri = that.namespace.getUri();
            
            if (thisNamespaceUri == null || thatNamespaceUri == null || thisNamespaceUri == thatNamespaceUri || thisNamespaceUri.equals(thatNamespaceUri))
            {
                if (thisPrefix == null || thatPrefix == null || thisPrefix == thatPrefix || thisPrefix.equals(thatPrefix))
                {
                    if (thisName == thatName || (thisName != null && thisName.equals(thatName)))
                    {
                        return true;
                    }                    
                }
            }
        }
        
        return false;
    }
}
